<template>
	<view class="content">
		<view class="search">
			<u-search v-model="text" shape="round" :show-action="true" action-text="搜索" :animation="true" @search="search"></u-search>
		</view>
		
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#000000" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false"
				 swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<u-card :title="item.articleName" :sub-title="$u.timeFormat(item.time, 'yyyy年mm月dd日')" v-for="(item, index) in  recommend"
							 :key="item.id" @click="toarticle(item.id)">
								<view class="" slot="body">
									<u-image width="100%" height="300rpx" :src="item.img"></u-image>
								</view>
								<view class="" slot="foot">
									<u-icon name="eye-fill" size="34" color="" :label="item.pageView+'浏览'"></u-icon>
								</view>
							</u-card>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<u-card :title="item.articleName" :sub-title="$u.timeFormat(item.time, 'yyyy年mm月dd日')" v-for="(item, index) in  newlist"
							 :key="item.id" @click="toarticle(item.id)">
								<view class="" slot="body">
									<u-image width="100%" height="300rpx" :src="item.img"></u-image>
								</view>
								<view class="" slot="foot">
									<u-icon name="eye-fill" size="34" color="" :label="item.pageView+'浏览'"></u-icon>
								</view>
							</u-card>
							<u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
						name: '推荐文章'
					},
					{
						name: '最新文章'
					}
				],
				recommend: [],
				newlist: [],
				current: 0,
				swiperCurrent: 0,
				pageNo1: 1,
				pageNo2: 1,
				loadStatus: ['loadmore', 'loadmore'],
				text:""
			}
		},
		onLoad() {
			this.getrecommendData();
			this.getnewData();
		},
		methods: {
			reachBottom() {
				if (this.current == 0) {
					if (this.loadStatus[this.current] != "nomore") {
						this.loadStatus.splice(this.current, 0, "loading")
						this.getrecommendData()
					}
				} else if (this.current == 1) {
					if (this.loadStatus[this.current] != "nomore") {
						this.loadStatus.splice(this.current, 1, "loading")
						this.getnewData()
					}
				}

			},
			// // 页面数据
			getrecommendData() {
				this.$u.api.postarticleslist({
					pageNo: this.pageNo1
				}).then(res => {
					console.log(res)
					res.data.forEach(item => {
						this.recommend.push(item)
					})
					//console.log(this.pageNo*4<res.num)
					if (this.pageNo1 * 4 < res.num) {
						this.loadStatus.splice(this.current, 0, "loadmore")
					} else {
						this.loadStatus.splice(this.current, 0, "nomore")
					}
					this.pageNo1++
				})
			},
			getnewData() {
				this.$u.api.postarticlesnewlist({
					pageNo: this.pageNo2
				}).then(res => {
					console.log(res)
					res.data.forEach(item => {
						this.newlist.push(item)
					})
					//console.log(this.pageNo*4!=res.num)
					if (this.pageNo2 * 4 < res.num) {
						this.loadStatus.splice(this.current, 1, "loadmore")
					} else {
						this.loadStatus.splice(this.current, 1, "nomore")
					}
					this.pageNo2++
				})
			},
			change(index) {
				this.swiperCurrent = index;
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			toarticle(id){
				this.$u.route('/pages/article/article', {
					id
				});
			},
			search(value){
				this.$u.route('/pages/search/search', {
					key:this.text
				});
			}
		},
	}
</script>
<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>
<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
	.search{
		background-color: #fff;
		padding-top: 10rpx;
		padding-left:20rpx ;
		padding-right: 20rpx;
	}
	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}

	.u-card-wrap {
		background-color: $u-bg-color;
		padding: 1px;
	}

	.u-body-item {
		font-size: 32rpx;
		color: #333;
		padding: 20rpx 10rpx;
	}

	.u-body-item image {
		width: 120rpx;
		flex: 0 0 120rpx;
		height: 120rpx;
		border-radius: 8rpx;
		margin-left: 12rpx;
	}
</style>
